##############################################
### read data #####

library('data.table')

d <- fread('annual_data_markups.csv',drop = 'V1')
d$fyear <- as.numeric(d$fyear)

d$date <- d$fyear

d <- d[cogs > 0 & sale > 0]

library('lfe')

N <- max(d$period)
source('../lag.R')

dl <- d[,.(period=1:N,
           sale.l1 = lag(sale, period, 1, N),
           emp.l1   = lag(emp, period, 1, N),
           cogs.l1  = lag(cogs, period, 1, N),
           xlr.l1   = lag(xlr, period, 1, N)), by = c('gvkey')]

d <- merge(d, dl, by = c('gvkey', 'period'))

d <- d[complete.cases(d[,c('sale','sale.l1','emp','emp.l1')])]

################################################
########
################################################

d$sale.g1 <- (d$sale - d$sale.l1)/(.5*d$sale + .5*d$sale.l1)
d$emp.g1 <- (d$emp - d$emp.l1)/(.5*d$emp + .5*d$emp.l1)
d$cogs.g1 <- (d$cogs - d$cogs.l1)/(.5*d$cogs + .5*d$cogs.l1)
d$xlr.g1 <- (d$xlr - d$xlr.l1)/(.5*d$xlr + .5*d$xlr.l1)

################
# Variance #
################

dd <- d[date == 2010]
100*var(dd$sale.g1, na.rm = TRUE)
100*var(dd$emp.g1, na.rm = TRUE)

###################################
# autocorrelation of growth rates #
###################################

dl <- d[,.(period=1:N,
           sale.g1.l1 = lag(sale.g1, period, 1, N),
           emp.g1.l1   = lag(emp.g1, period, 1, N)), by = c('gvkey')]

d <- merge(d, dl, by = c('gvkey', 'period'))

d <- d[complete.cases(d[,c('sale.g1.l1', 'emp.g1.l1')])]

cor(d$sale.g1, d$sale.g1.l1)
cor(d$emp.g1, d$emp.g1.l1)

